package com.example.mpandroidchart_all.FragmentChart;

import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.mpandroidchart_all.Market.TMarket;
import com.example.mpandroidchart_all.R;
import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;

import java.util.ArrayList;
import java.util.List;


public class FragmentLineChart extends Fragment {
    private LineChart lineChart;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_linechart,container,false);
        lineChart=view.findViewById(R.id.line_chart);

        setXAxis();
        setYAxis();
        initData();

        return view;
    }
    private void setXAxis(){
        XAxis xAxis = lineChart.getXAxis();
        xAxis.setAxisMinimum(0f);
        xAxis.setAxisMaximum(63f);
        xAxis.setLabelCount(20);
        xAxis.setDrawGridLines(false);
        xAxis.setTextColor(Color.WHITE);
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);

    }
    private void setYAxis(){
        YAxis axisLeft = lineChart.getAxisLeft();
        axisLeft.setTextColor(Color.WHITE);
        axisLeft.setAxisMaximum(26);
        axisLeft.setAxisMinimum(14);
        lineChart.getAxisRight().setEnabled(false);
    }

    private void initData() {
        lineChart.setExtraOffsets(24f,24f,24f,0f);
        lineChart.setBackgroundColor(Color.parseColor("#0f0f0f"));
        setChartData();
    }
    private void setChartData(){
        List<Entry> yVals1 = new ArrayList<>();
        float[] ys1 = new float[] {
                19f, 19f, 18f, 18f, 18f, 18f, 17f, 16f, 17f, 19f,
                21f, 21f, 23f, 23f, 24f, 24f, 25f, 25f, 25f, 24f};
        for (int i = 0; i < ys1.length; i++) {
            yVals1.add(new Entry((i + 1) * 3,ys1[i]));
        }
        LineDataSet set1=new LineDataSet(yVals1,"");
        set1.setValueTextColor(Color.WHITE);
        set1.setDrawFilled(true);
        set1.setFillColor(Color.WHITE);
        LineData lineData=new LineData(set1);
        TMarket tMarket=new TMarket();
        lineChart.setMarker(tMarket);
        lineChart.setData(lineData);
        lineChart.invalidate();
    }
}
